package com.ruoyi.web.controller.system;

import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Orders;
import com.ruoyi.system.service.IOrdersService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 订单管理 Controller
 * 
 * @author liukangzu
 * @date 2025-04-17
 */
@RestController
@RequestMapping("/system/orders")
public class OrdersController extends BaseController
{
    @Autowired
    private IOrdersService ordersService;

    /**
     * 查询订单管理 列表
     */
    @PreAuthorize("@ss.hasPermi('system:orders:list')")
    @GetMapping("/list")
    public TableDataInfo list(Orders orders)
    {
        startPage();
        List<Orders> list = ordersService.selectOrdersList(orders);
        System.out.println(list.toString());
        return getDataTable(list);
    }

    /**
     * 导出订单管理 列表
     */
    @PreAuthorize("@ss.hasPermi('system:orders:export')")
    @Log(title = "订单管理 ", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Orders orders)
    {
        List<Orders> list = ordersService.selectOrdersList(orders);
        ExcelUtil<Orders> util = new ExcelUtil<Orders>(Orders.class);
        util.exportExcel(response, list, "订单管理 数据");
    }

    /**
     * 获取订单管理 详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:orders:query')")
    @GetMapping(value = "/{orderId}")
    public AjaxResult getInfo(@PathVariable("orderId") Integer orderId)
    {
        return success(ordersService.selectOrdersByOrderId(orderId));
    }

    /**
     * 新增订单管理 
     */
    @PreAuthorize("@ss.hasPermi('system:orders:add')")
    @Log(title = "订单管理 ", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Orders orders)
    {
        return toAjax(ordersService.insertOrders(orders));
    }

    /**
     * 修改订单管理 
     */
    @PreAuthorize("@ss.hasPermi('system:orders:edit')")
    @Log(title = "订单管理 ", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Orders orders)
    {
        return toAjax(ordersService.updateOrders(orders));
    }

    /**
     * 删除订单管理 
     */
    @PreAuthorize("@ss.hasPermi('system:orders:remove')")
    @Log(title = "订单管理 ", businessType = BusinessType.DELETE)
	@DeleteMapping("/{orderIds}")
    public AjaxResult remove(@PathVariable Integer[] orderIds)
    {
        return toAjax(ordersService.deleteOrdersByOrderIds(orderIds));
    }

    /**
     * 关闭订单管理
     */
    @PreAuthorize("@ss.hasPermi('system:orders:close')")
    @Log(title = "订单管理", businessType = BusinessType.UPDATE)
    @PutMapping("/close")
    public AjaxResult close(@RequestParam String orderIds)
    {
        Integer[] ids = Arrays.stream(orderIds.split(","))
                .map(Integer::valueOf)
                .toArray(Integer[]::new);

        return toAjax(ordersService.closeOrderByOrderIds(ids));
    }

    /**
     * 配货订单管理
     */
    @PreAuthorize("@ss.hasPermi('system:orders:distribution')")
    @Log(title = "订单管理", businessType = BusinessType.UPDATE)
    @PutMapping("/distribution")
    public AjaxResult distribution(@RequestParam Integer orderId)
    {
        return ordersService.distributionOrderByOrderIds(orderId);
    }
}
